<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>文章管理</el-breadcrumb-item>
				<el-breadcrumb-item>编辑类别</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-box">
			<el-form ref="editForm1" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
				<el-tabs v-model="activeName" type="card">
					<el-tab-pane label="基本信息" name="info">
						<div class="tab-content">
							<el-form-item label="是否启用">
								<el-switch :active-value="0" :inactive-value="1" v-model="model.status"></el-switch>
							</el-form-item>
							<el-form-item prop="parentId" label="所属父类别">
								<dt-dropdown-select v-model="model.parentId" :disabled="model.id" :data="parentList" placeholder="请选择"></dt-dropdown-select>
							</el-form-item>
							<el-form-item prop="title" label="类别名称">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="sortId" label="排序数字">
								<el-input type="number" v-model="model.sortId" placeholder="数字越小越排前"></el-input>
							</el-form-item>
							<el-form-item label="创建时间" v-if="model.id>0">
								{{model.addBy}} {{model.addTime}}
							</el-form-item>
						</div>
					</el-tab-pane>
					<el-tab-pane label="扩展选项" name="expand">
						<div class="tab-content">
							<el-form-item label="显示图片">
								<dt-upload v-model="model.imgUrl"></dt-upload>
							</el-form-item>
							<el-form-item label="类别介绍">
								<el-input type="textarea" :rows="5" v-model="model.content" placeholder="可空，详细描述及介绍"></el-input>
							</el-form-item>
						</div>
					</el-tab-pane>
				</el-tabs>
			</el-form>
		</div>
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" :loading="loading" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	import DtDropdownSelect from '../../../components/DtDropdownSelect.vue'
	import DtUpload from '../../../components/DtUpload.vue'
	
	export default {
		components: {
			DtDropdownSelect,
			DtUpload
		},
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				loading: false,
				parentList: [], //父类别列表
				model: {
					id: 0,
					parentId: 0,
					title: null,
					imgUrl: null,
					content: null,
					sortId: 99,
					status: 0,
					addBy: null,
					addTime: null,
				},
				rules: {
					title: [
						{ required: true, message: '请输入分类名称', trigger: 'blur' },
					],
					sortId: [
						{ required: true, message: '请输入排序数字', trigger: 'blur' },
						{ pattern: /^[+]{0,1}(\d+)$/, message: '只能为正整数', trigger: 'blur' },
					],
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this; //当前页面
				let parentId = _this.$route.query.parentId; //获取父ID
				if (_this.id) {
					_this.$api.request({
						url: `/admin/article/category/${_this.id}`,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						},
					});
				} else if (parentId) {
					_this.model.parentId = parseInt(parentId);
				}
				//加载父类列表
				_this.$api.request({
					url: '/admin/article/category/',
					loading: true,
					success(res) {
						_this.parentList = res.data;
					}
				});
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm1'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//如果是数组则取最后一个值
					if (Array.isArray(_this.model.parentId)) {
						_this.model.parentId = _this.model.parentId[_this.model.parentId.length - 1];
					}
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: `/admin/article/category/${_this.model.id}`,
							data: _this.model,
							successMsg: '修改类别已成功',
							beforeSend() {
								_this.loading = true;
							},
							success(res) {
								//可跳转加列表页
								_this.$common.linkUrl('/article/category/list');
							},
							complete() {
								_this.loading = false
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/article/category',
							data: _this.model,
							successMsg: '新增类别已成功',
							beforeSend() {
								_this.loading = true;
							},
							success(res) {
								//重置表单
								_this.$refs['editForm1'].resetFields();
								_this.initData();
							},
							complete() {
								_this.loading = false
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>

<style lang="scss">
</style>